<template>
	<view class="my-order-main">

		<!-- 头部返回和搜索框 -->
		<view class="retuen-icon-search">
			<view class="return-icon" @click="returnPreviewPage">
				<uni-icons type="left" size="26"></uni-icons>
			</view>
			<view class="order-search">
				<uni-icons type="search" size="22" color="#a7a7a7"></uni-icons>
				<input type="text" v-model="orderSearchValue" @confirm="searchOrders" placeholder="搜索我的订单" />
			</view>
		</view>

		<!-- 订单列表 -->
		<view class="order-list-content">
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text"
				activeColor="#66596b"></uni-segmented-control>
			<view class="content">
				<view v-show="current === 0">
					<view class="all-order-empty" v-if="allOrders.length <= 0">
						您还没有订单！
					</view>
					<view class="all-order" v-else>
						<view class="all-order-item" v-for="(item,index) in allOrders" :key="index">
							<view class="order-image" @click="toOrderDetailPage(item)">
								<image :src="item.orderImg" mode="aspectFill"></image>
								<view class="order-info">
									<view class="order-info-top">
										<view class="order-title-price">
											<view class="order-title">
												{{item.orderTitle}}
											</view>
											<view class="order-price">
												{{item.goodsSiglePrice}}
											</view>
										</view>
										<view class="order-style-text">
											<view class="order-style-content">
												{{ item.styles }}
											</view>
											<view class="order-buy-count">
												X{{item.count}}
											</view>
										</view>
									</view>
									<view class="goods-count-pay">
										<view class="goods-count-pay-left">
											共{{item.count}}件
										</view>
										<view class="goods-count-pay-right">
											<view class="goods-pay-state">
												{{item.orderState}}
											</view>
											<view class="goods-count-pay-price">
												{{item.orderPrice}}
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="order-operation">
								<view class="order-operation-left" @click="clickOperationDot(item)">
									<view class="order-dots">
										<uni-icons type="more-filled" size="22" color="#666666"></uni-icons>
									</view>
								</view>
								<view class="order-operation-right">
									<button type="default" class="edit-address" @click="updateAddress(item)"
										v-if="item.orderState === '待付款' || item.orderState === '实付款'">修改地址</button>
									<button type="default" class="pay-btn"
										v-if="item.orderState === '待付款'">立即支付{{countDown}}</button>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-show="current === 1">
					<view class="all-order-empty" v-if="pendingPayMents.length <= 0">
						您还没有待付款订单！
					</view>
					<view class="all-order" v-else>
						<view class="all-order-item" v-for="(item,index) in pendingPayMents" :key="index">
							<view class="order-image" @click="toOrderDetailPage(item)">
								<image :src="item.orderImg" mode="aspectFill"></image>
								<view class="order-info">
									<view class="order-info-top">
										<view class="order-title-price">
											<view class="order-title">
												{{item.orderTitle}}
											</view>
											<view class="order-price">
												{{item.goodsSiglePrice}}
											</view>
										</view>
										<view class="order-style-text">
											<view class="order-style-content">
												{{ item.styles }}
											</view>
											<view class="order-buy-count">
												X{{item.count}}
											</view>
										</view>
									</view>
									<view class="goods-count-pay">
										<view class="goods-count-pay-left">
											共{{item.count}}件
										</view>
										<view class="goods-count-pay-right">
											<view class="goods-pay-state">
												{{item.orderState}}
											</view>
											<view class="goods-count-pay-price">
												{{item.orderPrice}}
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="order-operation">
								<view class="order-operation-left" @click="clickOperationDot">
									<view class="order-dots">
										<uni-icons type="more-filled" size="22" color="#666666"></uni-icons>
									</view>
								</view>
								<view class="order-operation-right">
									<button type="default" class="edit-address"
										@click="updateAddress(item)">修改地址</button>
									<button type="default" class="pay-btn">立即支付{{countDown}}</button>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-show="current === 2">
					<view class="all-order-empty" v-if="pendingShipments.length <= 0">
						您还没有待发货订单！
					</view>
					<view class="all-order" v-else>
						<view class="all-order-item" v-for="(item,index) in pendingShipments" :key="index">
							<view class="order-image" @click="toOrderDetailPage(item)">
								<image :src="item.orderImg" mode="aspectFill"></image>
								<view class="order-info">
									<view class="order-info-top">
										<view class="order-title-price">
											<view class="order-title">
												{{item.orderTitle}}
											</view>
											<view class="order-price">
												{{item.goodsSiglePrice}}
											</view>
										</view>
										<view class="order-style-text">
											<view class="order-style-content">
												{{ item.styles }}
											</view>
											<view class="order-buy-count">
												X{{item.count}}
											</view>
										</view>
									</view>
									<view class="goods-count-pay">
										<view class="goods-count-pay-left">
											共{{item.count}}件
										</view>
										<view class="goods-count-pay-right">
											<view class="goods-pay-state">
												{{item.orderState}}
											</view>
											<view class="goods-count-pay-price">
												{{item.orderPrice}}
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="order-operation">
								<view class="order-operation-left" @click="clickOperationDot">
									<view class="order-dots">
										<uni-icons type="more-filled" size="22" color="#666666"></uni-icons>
									</view>
								</view>
								<view class="order-operation-right">
									<button type="default" class="edit-address"
										@click="updateAddress(item)">修改地址</button>
									<button type="default" class="pay-btn">取消订单</button>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-show="current === 3">
					<view class="all-order-empty" v-if="pendingReceipts.length <= 0">
						您还没有待收货订单！
					</view>
					<view class="all-order" v-else>
						<view class="all-order-item" v-for="(item,index) in pendingReceipts" :key="index">
							<view class="order-image" @click="toOrderDetailPage(item)">
								<image :src="item.orderImg" mode="aspectFill"></image>
								<view class="order-info">
									<view class="order-info-top">
										<view class="order-title-price">
											<view class="order-title">
												{{item.orderTitle}}
											</view>
											<view class="order-price">
												{{item.goodsSiglePrice}}
											</view>
										</view>
										<view class="order-style-text">
											<view class="order-style-content">
												{{ item.styles }}
											</view>
											<view class="order-buy-count">
												X{{item.count}}
											</view>
										</view>
									</view>
									<view class="goods-count-pay">
										<view class="goods-count-pay-left">
											共{{item.count}}件
										</view>
										<view class="goods-count-pay-right">
											<view class="goods-pay-state">
												{{item.orderState}}
											</view>
											<view class="goods-count-pay-price">
												{{item.orderPrice}}
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="order-operation">
								<view class="order-operation-left" @click="clickOperationDot">
									<view class="order-dots">
										<uni-icons type="more-filled" size="22" color="#666666"></uni-icons>
									</view>
								</view>
								<view class="order-operation-right">
									<button type="default" class="pay-btn">确认收货</button>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-show="current === 4">
					<view class="all-order-empty" v-if="unEvaluatedList.length <= 0">
						您还没有待评价的订单！
					</view>
					<view class="all-order" v-else>
						<view class="all-order-item" v-for="(item,index) in unEvaluatedList" :key="index">
							<view class="order-image" @click="toOrderDetailPage(item)">
								<image :src="item.orderImg" mode="aspectFill"></image>
								<view class="order-info">
									<view class="order-info-top">
										<view class="order-title-price">
											<view class="order-title">
												{{item.orderTitle}}
											</view>
											<view class="order-price">
												{{item.goodsSiglePrice}}
											</view>
										</view>
										<view class="order-style-text">
											<view class="order-style-content">
												{{ item.styles }}
											</view>
											<view class="order-buy-count">
												X{{item.count}}
											</view>
										</view>
									</view>
									<view class="goods-count-pay">
										<view class="goods-count-pay-left">
											共{{item.count}}件
										</view>
										<view class="goods-count-pay-right">
											<view class="goods-pay-state">
												{{item.orderState}}
											</view>
											<view class="goods-count-pay-price">
												{{item.orderPrice}}
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="order-operation">
								<view class="order-operation-left" @click="clickOperationDot">
									<view class="order-dots">
										<uni-icons type="more-filled" size="22" color="#666666"></uni-icons>
									</view>
								</view>
								<view class="order-operation-right">
									<button type="default" class="to-evaluate">去评价</button>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 弹窗 -->
		<uni-popup ref="operaPopup" type="bottom" border-radius="10px 10px 0 0">
			<view class="opera-popup">
				<view class="opera-item" v-for="(item,index) in dotOpera" :key="index" @click="toOperaOrder(item)">
					<view class="cancel-order-image">
						<uni-icons type="trash-filled" size="26" v-if="item.text === '删除订单'" color="#fff"></uni-icons>
						<image :src="item.icon" mode="aspectFill" v-else></image>
					</view>
					<view class="cancel-order-text">
						{{item.text}}
					</view>
				</view>
			</view>
		</uni-popup>

		<!-- 取消订单弹窗 -->
		<uni-popup ref="cancelOrderPop" type="bottom" border-radius="10px 10px 0 0" :mask-click="false">
			<cancel-order @click-cloes="closeOrderCancelPop"></cancel-order>
		</uni-popup>
		
		<!-- 售后类型弹窗 -->
		<uni-popup ref="afterSalesPop" type="bottom" border-radius="10px 10px 0 0" :mask-click="false">
			<after-sale @click-sftersale="closeAfterSale"></after-sale>
		</uni-popup>

	</view>
</template>

<script>
	import {
		getOrderList
	} from '@/request/order.js'
	import CancelOrder from './cancelOrder.vue';
	import afterSale from './afterSale.vue';
import { all } from 'axios';
	export default {
		components: {
			CancelOrder,
			afterSale
		},
		data() {
			return {
				allOrderOpera: [{
						icon: '../../static/icons/customer-service.png',
						text: '联系客服'
					},
					{
						icon: '../../static/icons/cancel-order.png',
						text: '取消订单'
					},
					{
						icon: 'trash-filled',
						text: '删除订单'
					},
					{
						icon: '../../static/icons/after-sale.png',
						text: '申请售后'
					},
					{
						icon: '../../static/icons/evaluate.png',
						text: '去评价'
					}
				],
				dotOpera: [],
				totalSeconds: 1800, // 初始总秒数，代表半小时，可根据需求调整时长
				timer: null, // 定时器标识，用于后续清除定时器
				countDown: '30:00', // 半小时倒计时
				orderSearchValue: '', // 订单搜索关键字
				items: ['全部', '待付款', '待发货', '待收货', '评价'],
				current: 0,
				allOrderEmpty: false,
				allOrders: [{
						orderAid: 1,
						orderImg: 'https://img2.baidu.com/it/u=1517393431,1288835989&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						orderTitle: '秋冬季新款针织毛衣...',
						goodsSiglePrice: '￥108',
						styles: '单件毛衣灰色M8165 2XL',
						count: 1,
						orderPrice: '￥108',
						orderState: '待付款',
					},
					{
						orderAid: 2,
						orderImg: 'https://img2.baidu.com/it/u=1517393431,1288835989&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						orderTitle: '秋冬季新款针织毛衣...',
						goodsSiglePrice: '￥108',
						styles: '单件毛衣灰色M8165 2XL',
						count: 1,
						orderPrice: '￥108',
						orderState: '待付款',
					},
				],
				pendingPayMents: [{
					orderAid: 2,
					orderImg: 'https://img2.baidu.com/it/u=1517393431,1288835989&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
					orderTitle: '秋冬季新款针织毛衣...',
					goodsSiglePrice: '￥108',
					styles: '单件毛衣灰色M8165 2XL',
					count: 1,
					orderPrice: '￥108',
					orderState: '待付款',
				}, ],
				pendingShipments: [{
					orderAid: 2,
					orderImg: 'https://img2.baidu.com/it/u=1517393431,1288835989&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
					orderTitle: '秋冬季新款针织毛衣...',
					goodsSiglePrice: '￥108',
					styles: '单件毛衣灰色M8165 2XL',
					count: 1,
					orderPrice: '￥108',
					orderState: '实付款',
				}, ],
				pendingReceipts: [{
					orderAid: 2,
					orderImg: 'https://img2.baidu.com/it/u=1517393431,1288835989&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
					orderTitle: '秋冬季新款针织毛衣...',
					goodsSiglePrice: '￥108',
					styles: '单件毛衣灰色M8165 2XL',
					count: 1,
					orderPrice: '￥108',
					orderState: '已发货',
				}, ],
				unEvaluatedList: [{
					orderAid: 2,
					orderImg: 'https://img2.baidu.com/it/u=1517393431,1288835989&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
					orderTitle: '秋冬季新款针织毛衣...',
					goodsSiglePrice: '￥108',
					styles: '单件毛衣灰色M8165 2XL',
					count: 1,
					orderPrice: '￥108',
					orderState: '已收货',
				}],
				currentOperationalOrder: {}, // 当前操作的订单
			};
		},

		mounted() {
			// 获取订单列表
			this.getOrderListFunc();
			this.startCountdown();
		},

		methods: {

			// 获取订单列表
			getOrderListFunc() {
				// getOrderList().then(res=>{
				// console.log(res);

				// 改变segment的内容
				this.changeSegmentContent();

				// })
			},

			// 改变segment的内容
			changeSegmentContent() {
				this.items[0] = '全部' + (this.allOrders.length > 0 ? this.allOrders.length : '');
				this.items[1] = '待付款' + (this.pendingPayMents.length > 0 ? this.pendingPayMents.length : '');
				this.items[2] = '待发货' + (this.pendingShipments.length > 0 ? this.pendingShipments.length : '');
				this.items[3] = '待收货' + (this.pendingReceipts.length > 0 ? this.pendingReceipts.length : '');
				this.items[4] = '评价' + (this.unEvaluatedList.length > 0 ? this.unEvaluatedList.length : '');
			},

			// 启动倒计时
			startCountdown() {
				this.timer = setInterval(() => {
					if (this.totalSeconds > 0) {
						this.totalSeconds--;
						this.updateCountDownText();
					} else {
						clearInterval(this.timer);
						// 倒计时结束后的逻辑，比如改变按钮文字等，这里简单示例改变显示文本
						this.countDown = '';
					}
				}, 1000);
			},

			// 更新倒计时显示文本格式（将秒数转换为分:秒格式）
			updateCountDownText() {
				const minutes = Math.floor(this.totalSeconds / 60);
				const seconds = this.totalSeconds % 60;
				this.countDown = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
			},

			// 搜索订单
			searchOrders() {
				const keyword = this.orderSearchValue.trim().toLowerCase(); // 获取输入的关键字并去除空格转换为小写，方便后续比较
				if (keyword === '') {
					// 如果关键字为空，还原各个订单列表原始数据（可根据实际需求看是否需要这步）
					this.getOrderListFunc();
					return;
				}
				if (this.current === 0) {
					// 搜索全部
					// 筛选全部订单列表
					this.allOrders = this.allOrders.filter(order => {
						return order.orderTitle.toLowerCase().includes(keyword) ||
							order.styles.toLowerCase().includes(keyword) ||
							order.orderState.toLowerCase().includes(keyword);
					});
				} else if (this.current === 1) {
					// 筛选待付款订单列表
					this.pendingPayMents = this.pendingPayMents.filter(order => {
						return order.orderTitle.toLowerCase().includes(keyword) ||
							order.styles.toLowerCase().includes(keyword) ||
							order.orderState.toLowerCase().includes(keyword);
					});
				} else if (this.current === 2) {
					// 筛选待发货订单列表
					this.pendingShipments = this.pendingShipments.filter(order => {
						return order.orderTitle.toLowerCase().includes(keyword) ||
							order.styles.toLowerCase().includes(keyword) ||
							order.orderState.toLowerCase().includes(keyword);
					});
				} else if (this.current === 3) {
					// 筛选待收货订单列表
					this.pendingReceipts = this.pendingReceipts.filter(order => {
						return order.orderTitle.toLowerCase().includes(keyword) ||
							order.styles.toLowerCase().includes(keyword) ||
							order.orderState.toLowerCase().includes(keyword);
					});
				} else if (this.current === 4) {
					// 筛选待评价订单列表
					this.unEvaluatedList = this.unEvaluatedList.filter(order => {
						return order.orderTitle.toLowerCase().includes(keyword) ||
							order.styles.toLowerCase().includes(keyword) ||
							order.orderState.toLowerCase().includes(keyword);
					});
				}
				// 改变segment的内容
				this.changeSegmentContent();
			},

			// 返回上一页
			returnPreviewPage() {
				uni.navigateBack()
			},

			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
			},

			updateAddress(item) {
				console.log(item);
				uni.navigateTo({
					url: `/pages/tabBar/template/createNewAddressPage?orderAid=${item.orderAid}`
				})
			},

			clickOperationDot(item) {
				console.log(item);
				this.currentOperationalOrder = item;
				this.dotOpera = [];
				if (this.current === 0) {
					this.dotOpera = this.allOrderOpera;
				} else if (this.current === 1) {
					this.dotOpera.push(this.allOrderOpera[0]);
					this.dotOpera.push(this.allOrderOpera[1]);
				} else if (this.current === 2) {
					this.dotOpera.push(this.allOrderOpera[0]);
				} else if (this.current === 3) {
					this.dotOpera.push(this.allOrderOpera[0]);
					this.dotOpera.push(this.allOrderOpera[2]);
					this.dotOpera.push(this.allOrderOpera[3]);
				} else {
					this.dotOpera.push(this.allOrderOpera[0]);
					this.dotOpera.push(this.allOrderOpera[2]);
					this.dotOpera.push(this.allOrderOpera[3]);
				}
				this.$refs.operaPopup.open();
			},

			toOperaOrder(item) {
				const that = this;
				if (item.text === '联系客服') {
					uni.navigateTo({
						url: '/pages/friendsChat/chatPage/chatRoomPage'
					})
				} else if (item.text === '取消订单') {
					that.$refs.cancelOrderPop.open()
				} else if (item.text === '删除订单') {
					// 判断这个订单是否可以删除
					uni.showModal({
						title: '提示',
						content: '确定删除该订单吗？',
						cancelText: '再想想',
						confirmColor: '#66596b',
						success: function(res) {
							if (res.confirm) {
								that.allOrders = that.allOrders.filter(obj => obj.orderAid !== item.orderAid);
								that.currentOperationalOrder = {};
								that.getOrderListFunc();
							}
						}
					})
				} else if (item.text === '申请售后'){
					that.$refs.afterSalesPop.open()
				}
			},

			closeOrderCancelPop() {
				this.$refs.cancelOrderPop.close()
			},
			
			closeAfterSale(){
				this.$refs.afterSalesPop.close()
			},
			
			toOrderDetailPage(item){
				uni.navigateTo({
					url: `/pages/order/orderDetail?orderAid=${item.orderAid}`
				})
			},

		},
	};
</script>

<style lang="scss" scoped>
	.my-order-main {
		padding-top: 60rpx;

		.retuen-icon-search {
			width: 90%;
			margin-left: auto;
			margin-right: auto;
			display: flex;
			align-items: center;

			.order-search {
				background-color: #ffffff;
				width: 70%;
				height: 60rpx;
				margin-left: 30rpx;
				padding-left: 10rpx;
				display: flex;
				align-items: center;
				border-radius: 30rpx;

				input {
					margin-left: 10rpx;
					font-size: 25rpx;
					letter-spacing: 3rpx;
					width: 85%;
				}
			}
		}

		.order-list-content {
			margin-top: 30rpx;

			.content {
				width: 90%;
				margin-top: 30rpx;
				margin-left: auto;
				margin-right: auto;

				.all-order-empty {
					width: 100%;
					height: 400rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #7f7f7f;
					font-size: 20rpx;
					letter-spacing: 2rpx;
				}

				.all-order {

					.all-order-item {
						background-color: #ffffff;
						padding: 20rpx;
						margin-bottom: 40rpx;
						border-radius: 20rpx;
						box-shadow: 0 0 10px 5px rgba(209, 209, 209, 0.6);

						.order-image {
							display: flex;

							image {
								width: 160rpx;
								height: 160rpx;
								border-radius: 10rpx;
							}

							.order-info {
								margin-left: 20rpx;
								display: flex;
								flex-direction: column;
								justify-content: space-between;
								width: 72%;

								.order-info-top {
									.order-title-price {
										display: flex;
										justify-content: space-between;

										.order-title {
											font-size: 30rpx;
											font-weight: bold;
											letter-spacing: 2rpx;
										}

										.order-price {
											font-size: 34rpx;
										}
									}

									.order-style-text {
										display: flex;
										justify-content: space-between;
										margin-top: 10rpx;
										font-size: 28rpx;
										color: #666666;

										.order-style-content {}

										.order-buy-count {}
									}
								}

								.goods-count-pay {
									display: flex;
									justify-content: space-between;

									.goods-count-pay-left {
										font-size: 28rpx;
										color: #666666;
									}

									.goods-count-pay-right {
										display: flex;
										align-items: center;

										.goods-pay-state {
											color: #66596b;
											font-size: 25rpx;
										}

										.goods-count-pay-price {
											margin-left: 15rpx;
											font-size: 34rpx;
										}
									}
								}
							}
						}

						.order-operation {
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-top: 20rpx;

							.order-operation-left {
								display: flex;
								margin-left: 20rpx;
								width: 60rpx;
							}

							.order-operation-right {
								display: flex;

								button {
									width: 200rpx;
									border-radius: 50rpx;
									margin-left: 20rpx;
									font-size: 26rpx;
								}

								.edit-address {
									width: auto;
									background-color: #fff;
									border: 1rpx solid #66596b;
								}

								.pay-btn {
									width: auto;
									background-color: #66596b;
									color: #fff;
								}

								.to-evaluate {
									width: 160rpx;
									letter-spacing: 2rpx;
									background-color: #66596b;
									color: #fff;
								}
							}
						}
					}
				}
			}
		}

		.opera-popup {
			width: 100%;
			border-radius: 10px 10px 0 0;
			background-color: #f8f8f8;
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			align-items: center;

			.opera-item {
				padding: 30rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 20rpx;

				.cancel-order-image {
					width: 100rpx;
					height: 100rpx;
					background-color: #66596b;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 40rpx;
						height: 40rpx;
					}
				}

				.cancel-order-text {
					margin-top: 20rpx;
					font-size: 25rpx;
					text-align: center;
				}
			}
		}
	}
</style>